// pages/myCard/myCard.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    req: false,
    band: false,
    customVal: '',
    remarkShow: false,
    toEdit: false,
    userInfo: {},
    loginShow: false,
    login: false,
    moreshow:false,
    cardId:'',
    pageType:'',
    name:'',
  },
  moreshow(){
    this.setData({
      moreshow:true
    })
  },
  morehide(){
    this.setData({
      moreshow: false
    })
  },
  login() {
    this.toggle('loginShow', true)
    this.selectComponent('#login').showClose()
  },
  loginClose() {
    this.toggle('loginShow', false)
  },
  toggleToast(e) {
    console.log(e.detail)
    if (!e.detail.loginShow) {
      this.toggle('loginShow', false)
    }
    this.getUserInfo()
  },
  toorg(e){
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/orgHome/orgHome?id='+id +'&mycard=1',
    })
  },
  call(e) {
    var dataset = e.currentTarget.dataset;
    wx.makePhoneCall({
      phoneNumber: dataset.tel
    })
  },
  band: function() {
    var that = this;
    app.req({
      url: "orgmini/FriendVisitor/blackMaskedState",
      data: {
        visitorId: that.data.cardId
      },
      success: function(res) {
        if (200 == res.code) {
          var data = res.data;
          that.toggle("band", data.pullBlack || data.shieldStatus);
        }
      }
    });
  },
  clickComment(e) {
    var dataset = e.currentTarget.dataset;
    this.toggle('commenyIndex', dataset.index)
    this.toggle('customVal', '')
    this.toggle('comment', dataset.content)
  },
  customComment(e) {
    this.toggle('commenyIndex', null)
    this.toggle('comment', e.detail.value)
  },
  remark() {
    var that = this;
    this.toggle('comment', '')
    getApp().mtj.trackEvent('yqlw_friend_comment');
    app.req({
      url: 'orgmini/FriendVisitor/reviewCheck',
      data: {
        viewId: that.data.cardId
      },
      success: function(res) {
        if (200 == res.code && !res.data) {
          that.toggle('remarkShow', true)
        } else if (200 == res.code && res.data) {
          that.toggle('remarkShow', true)
          that.toggle('comment', res.data.comment)
          that.toggle('toEdit', true)
          that.toggle('toEditId', res.data.id)
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      }
    });

  },
  remarkClose() {
    this.toggle('remarkShow', false)
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  toPage(e) {
    var dataset = e.currentTarget.dataset;
    var url = dataset.turn
    wx.navigateTo({
      url: url
    })
  },
  addFriend() {
    var that = this;
    app.req({
      url: 'orgmini/FriendVisitor/applyFriend',
      data: {
        uid: that.data.commentId,
        friendId: that.data.cardId,
      },
      success: function(res) {
        if (200 == res.code) {
          wx.showToast({
            title: '已发送申请',
            duration: 2000
          })
        }
      }
    });
  },
  addComment() {
    var that = this;
    that.toggle('commenyIndex', null)
    if (that.data.toEdit) {
      var url = 'orgmini/FriendVisitor/editReview'
      var param = {
        id: that.data.toEditId,
        comment: that.data.comment
      }
    } else {
      var url = 'orgmini/FriendVisitor/review'
      var param = {
        commentId: that.data.commentId,
        userId: that.data.cardId,
        comment: that.data.comment
      }
    }
    if (that.data.comment != '') {
      app.req({
        url: url,
        data: param,
        success: function(res) {
          if (200 == res.code) {
            that.toggle('remarkShow', false)
            that.list()
          }
        }
      });
    } else {
      that.toggle('remarkShow', false)
    }
  },
  clickAddComment(e) {
    var dataset = e.currentTarget.dataset;
    var that = this;
    that.toggle('comment', dataset.comment)
    app.req({
      url: 'orgmini/FriendVisitor/reviewCheck',
      data: {
        viewId: that.data.cardId
      },
      success: function(res) {
        if (200 == res.code && !res.data) {
          app.req({
            url: 'orgmini/FriendVisitor/review',
            data: {
              commentId: that.data.commentId,
              userId: that.data.cardId,
              comment: dataset.comment
            },
            success: function(res) {
              if (200 == res.code) {
                that.list()
              }
            }
          });
        } else if (200 == res.code && res.data) {
          app.req({
            url: 'orgmini/FriendVisitor/editReview',
            data: {
              id: res.data.id,
              comment: that.data.comment
            },
            success: function(res) {
              if (200 == res.code) {
                that.toggle('remarkShow', false)
                that.list()
              }
            }
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      }
    });
  },
  list: function(append) {
    var that = this;
    that.toggle('req', false)
    if (that.data.pageType) {
      var url = 'orgmini/FriendVisitor/getOtherBusinessCard?viewUid=' + that.data.cardId
      if (that.data.login) {
        var uid = that.data.commentId
      } else {
        var uid = ''
      }
      var param = {
        viewUid: that.data.cardId,
        viewUid: uid
      }
    } else {
      if (that.data.login) {
        var uid = that.data.commentId
      } else {
        var uid = ''
      }
      var url = 'orgmini/user/getNewOtherBusinessCard'
      var param = {
        viewUid: uid
      }
    }
    app.req({
      url: url,
      data: param,
      success: function(res) {
        console.log(res,12)
        if (200 == res.code) {
          var list = res.data;
          list.birthday = list.birthday.substring(0,10)
          // list.userOrgList = list.userOrgList.filter(function(item) {
          //   return item.isBusinessCard == true
          // })
          if (that.data.name == '' && that.data.pageType){
            wx.setNavigationBarTitle({
              title: res.data.realName + '的名片'
            })
          }
          that.toggle('list', list)
          console.log(that.data.list)
          that.toggle('req', true)
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    if (options.name) {
      that.toggle('name', options.name)
    }
    console.log('传值',options)
    if (options.type) {
      that.toggle("pageType", options.type)
      that.toggle("cardId", options.id)
    } else {
      app.getUserInfo(function(userInfo) {
        if (options.id) {
          that.toggle("cardId", options.id)
        }
        that.toggle("commentId", userInfo.id);
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    that.getUserInfo()
  },
  getUserInfo() {
    var that = this
    wx.getSetting({
      success(res) {
        // if (!res.authSetting['scope.userInfo'] || wx.getStorageSync('uid') == '') {
        if ( wx.getStorageSync('uid') == '') {
          that.toggle("login", false);
          wx.setNavigationBarTitle({
            title: that.data.name + '的名片'
          })
          that.list()
        } else {
          that.toggle("login", true);
          app.getUserInfo(function(userInfo) {
            if (that.data.cardId != '' && that.data.cardId != userInfo.id) {
              that.setData({
                pageType:'friend'
              })
              wx.setNavigationBarTitle({
                title: that.data.name + '的名片'
              })
              app.req({
                url: 'orgmini/FriendVisitor/addVisitorRecord',
                data: {
                  userId: that.data.cardId
                },
                success: function (res) {
                  console.log(res, '新增访客')
                 
                }
              });
              that.band();
            }else{
              wx.setNavigationBarTitle({
                title: '我的名片'
              })
            }
            that.toggle("commentId", userInfo.id);
            that.toggle("userInfo", userInfo);
            that.list()
          }, true);
        }
      },
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this
    that.getUserInfo()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    var cardId=''
    var name = ''
    if (this.data.cardId==''){
      cardId= this.data.commentId
      name = this.data.userInfo.realName
    }else{
      cardId = this.data.cardId
      name = this.data.name
    }
    return {
      path: '/pages/myCardNew/myCardNew?id=' + cardId + '&name=' + name + '&type=' + this.data.pageType
    }
  }
})